package nl.hr.minor.mobilelife.j1213.jlr.words;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class LetterTiles extends ArrayList<LetterTile> {

	private static final long serialVersionUID = 893977657169059662L;

	public LetterTiles() {
		super(8);
	}
	
	/**
	 * Sets the LetterTile.dropZoneIndex from the LetterTile having that index
	 * @param dropZoneIndex The position of the dropzone in it's arraylist
	 */
	public void clearDropZone(int dropZoneIndex){
		if(dropZoneIndex >= 0 && dropZoneIndex < 8) {
			for(LetterTile lt : this) {
				if(lt.dropZoneIndex == dropZoneIndex) {
					lt.dropZoneIndex = LetterTile.DROPZONE_INDEX_NO_POSITION;
				}
			}
		}
	}
	
	/**
	 * Generates empty tiles, to be used with LevelPanel.init()
	 * @param context
	 */
	public void generateTiles(Context context){
		for(int i = 0; i < 8; i++)
			add(new LetterTile(context));
	}
	
	/**
	 * Reconfigures all tiles with a new position and width/height, based on the new word.
	 * In order to be used with LevelPanel.setWord()
	 * @param newWord The given word
	 * @param startY Position on the Y axis to draw on
	 * @param totalPanelWidth Total width of the surfaceview/canvas
	 */
	public void doNewWord(LinearLayout view, String newWord, int startY, int totalPanelWidth){
		int 
		newWordLength = newWord.length(),
		tileWidthHeight = (int) (totalPanelWidth * 0.8) / newWordLength; // all tiles (w/o margin) consume 80% of totalPanelWidth
		
		view.removeAllViews();
		
		for(int i = 0; i < 8; i++){
			
			LetterTile lt = get(i);
			if(i < newWordLength) {
				lt.setVisibility(View.VISIBLE);
				LinearLayout.LayoutParams lp = (LayoutParams) lt.getLayoutParams(); 
				lp.width = tileWidthHeight;
				lp.height = tileWidthHeight;
				lp.weight = 100 / newWordLength;
				lt.setLayoutParams(lp);
				lp = null;
				lt.setText(Character.toString(newWord.charAt(i)));
			} else { 
				lt.setVisibility(View.GONE);
			}
			
			view.addView(lt);
		}
	}
}
